package src.cs2340.todoApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

public class NewLocationScreen extends Activity {
	private String username;
	private String password;
	private EditText tag;
	private EditText streetNum;
	private EditText streetName;
	private EditText city;
	private AutoCompleteTextView stateView;
	private EditText zipcode;
	private String[] states;
	private String state;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newlocationscreen);
		
		tag = (EditText) findViewById(R.id.tagField);
		streetNum = (EditText) findViewById(R.id.streetNumField);
		streetName = (EditText) findViewById(R.id.streetNameField);
		city = (EditText) findViewById(R.id.city_Field);
		zipcode = (EditText) findViewById(R.id.zipcodeField);
		
		Button save = (Button) findViewById(R.id.saveLocButton);
		Button cancel = (Button) findViewById(R.id.cancelLocButton);
		
		states = new String[] {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN",
				"IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
				"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA",
				"WA", "WV", "WI", "WY"};
		ArrayAdapter<String> locAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, states);
				stateView = (AutoCompleteTextView) findViewById(R.id.stateAutoField);
				stateView.setAdapter(locAdapter);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
	    	username = extras.getString("username");
	    	password = extras.getString("password");
	    	
	    	Account currAccount = new Account(username, password, "", "", Database.getInstance());
			currAccount = Database.getInstance().getUpdate(currAccount);
	    }
		
		save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v){
				if(!tag.getText().toString().trim().equals("") && !streetNum.getText().toString().equals("")
						&& !streetName.getText().toString().trim().equals("") && !city.getText().toString().trim().equals("")
						&& !zipcode.getText().toString().trim().equals("")){
					if(!stateView.getText().toString().trim().equals("")){
						for(String s:states){
							if(stateView.getText().toString().equals(s)){
								state = s;
								break;
							}
						}
					}
					Location loc = new Location(tag.getText().toString(), Integer.parseInt(streetNum.getText().toString())
							, streetName.getText().toString(),	city.getText().toString(), stateView.getText().toString(), 
							Integer.parseInt(zipcode.getText().toString()));
					//TODO: Database.getInstance().addLoc(loc);
					LocationList.getLocs().add(loc);
					Intent resultIntent = new Intent();
					resultIntent.putExtra("Tag", tag.getText().toString());
					setResult(Activity.RESULT_OK, resultIntent);
					finish();
				}
				else{
					AlertDialog.Builder alertDialog = new AlertDialog.Builder(NewLocationScreen.this);
					alertDialog.setMessage("Please fill in all fields!");
					
					/**
					 * The code called for the "YES" option
					 */
					alertDialog.setPositiveButton("Okay!", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
				}
			}
		});
		
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(NewLocationScreen.this);
				alertDialog.setMessage("Are you sure you want to leave this screen and lose all changes?");
				
				/**
				 * The code called for the "YES" option
				 */
				alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				
				/**
				 * The code called for the "NO" option
				 */
				alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				})
				.show();
			}
		});
	}
}
